﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace System8ToyMC
{
	public partial class NamedValue : UserControl
	{
		public NamedValue()
		{
			// Required to initialize variables
			InitializeComponent();
            LayoutRoot.DataContext = this;

            CheckRO();
		}

        [Category("Numbers")]
        [Description("The name of the property we are looking at")]
        public string ValueName
        {
            get { return (string)GetValue(ValueNameProperty); }
            set { SetValue(ValueNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ValueName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueNameProperty =
            DependencyProperty.Register("ValueName", typeof(string), typeof(NamedValue), new PropertyMetadata("b Jets:"));

        [Category("Numbers")]
        [Description("The value of the property to show")]
        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(NamedValue), new PropertyMetadata(0.45));

        [Category("Numbers")]
        [Description("Can the user edit this guy or not?")]
        public bool ReadOnly
        {
            get { return (bool)GetValue(ReadOnlyProperty); }
            set { SetValue(ReadOnlyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ReadOnly.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ReadOnlyProperty =
            DependencyProperty.Register("ReadOnly", typeof(bool), typeof(NamedValue), new PropertyMetadata(false, new PropertyChangedCallback(ReadOnlyChanged)));

        public static void ReadOnlyChanged (DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            NamedValue nv = obj as NamedValue;
            if (nv != null)
            {
                nv.CheckRO();
            }
        }

        /// <summary>
        /// Check out Read Only and adjust.
        /// </summary>
        private void CheckRO()
        {
            if (ReadOnly)
            {
                TBROValue.Visibility = Visibility.Visible;
                TBValue.Visibility = Visibility.Collapsed;
            }
            else
            {
                TBROValue.Visibility = Visibility.Collapsed;
                TBValue.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// Hold onto the path for this variable
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// We just got the focus here. Help out the user by selecting everything.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TBValue_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            TBValue.SelectAll();
        }
    }
}